// greencuisine

// Ajax to get new restaurants
function updateRestaurants(divID, green, cuisine, offset, limit)
{
	if(limit > 25) limit = 25;
	
	var passData = '';
	passData += 'green='+pescape(green);
	passData += '&cuisine='+pescape(cuisine);
	passData += '&offset='+pescape(offset);
	passData += '&limit='+pescape(limit);
	
	var AJAX = null;
	if(window.XMLHttpRequest) AJAX=new XMLHttpRequest();
	else AJAX=new ActiveXObject("Microsoft.XMLHTTP");
	
	if(AJAX==null) {
	   alert("Your browser doesn't support AJAX.");                                         
	   return false;
	}
	else {
		AJAX.open("POST", "http://"+document.domain+"/restaurants.php", true);
		AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		AJAX.onreadystatechange = function() {                      
			if(AJAX.readyState==4 || AJAX.readyState=="complete") {
				callback(divID, green, cuisine, offset, limit, AJAX.responseText, AJAX.status);
			}
		}
		AJAX.send(passData);
	}
}

// Modifies restaurant div html
function updateHTML(divID, html)
{
	var div = getElem(divID);
	
	div.innerHTML = html;
}

// Produces html for page
function makeHTML(green, cuisine, json, offset, limit, total)
{
	var html = "<ul>";
	restaurants = eval(json);
	
	if(restaurants.length > 0)
	{
		var alt = false;
		for(var i = 0; i < restaurants.length; i++)
		{
			html += "<li";
			if(alt)
			{
				alt = false;
				html += " class=\"alt\"";
			}
			else alt = true;
			html += " onclick=\"showInfoById(" + restaurants[i].id + ")\"><h5>" + restaurants[i].name;
			if(restaurants[i].website != "") html += " (<a href=\"" + restaurants[i].website + "\">url</a>)";
			html += "</h5>" + restaurants[i].cuisine + "<br />" + betterDollars(restaurants[i].green) + "</li>";
		}
	
		html += "</ul>";
	
		if(offset > 0)
		{
			var newoffset = parseInt(offset) - parseInt(limit);
			if(offset < 0) offset = 0;
			html += "<a class=\"prev\" onclick=\"updateRestaurants('restaurants_data', '" + green + "', '" + cuisine + "', '" + newoffset + "', '" + limit + "')\"><img src=\"images/previous.png\" alt=\"previous\" /></a>";
		}
		var newoffset = parseInt(offset) + parseInt(limit);
		if(total > newoffset)
		{
			html += "<a class=\"next\" onclick=\"updateRestaurants('restaurants_data', '" + green + "', '" + cuisine + "', '" + newoffset + "', '" + limit + "')\"><img src=\"images/next.png\" alt=\"next\" /></a>";
		}
	}
	else
	{
		html = "<ul><li>No restaurants found with this criteria.</li></ul>";
	}
	
	return html;
}

// AJAX return call
function callback(divID, green, cuisine, offset, limit, serverData, serverStatus) {
	if(serverData != "error" && json != "")
	{
		var split = serverData.indexOf('|');
		var total = serverData.substring(0, split);
		var json = serverData.substring(split+1);

		// map.js
		updatePlaces(json);

		var html = makeHTML(green, cuisine, json, offset, limit, total);
		updateHTML(divID, html);
	}
	else alert('Unfortunately there was a problem getting additional restaurants.');
}

// Stupid plus sign does not escape
function pescape(str) {
	str = escape(str);
	str = str.replace(new RegExp( "\\+", "g" ), "%2B")
	return str;
}

// Pulldown box to call ajax
function getRestaurants(divID, greenID, cuisineID, limitID)
{
	green = getElem(greenID).value;
	cuisine = getElem(cuisineID).value;
	limit = getElem(limitID).value;
	
	updateRestaurants(divID, green, cuisine, 0, limit);
}
